package com.multysite.controller.admin;

import java.io.IOException;
import java.util.logging.Logger;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.appengine.api.NamespaceManager;
import com.google.appengine.api.users.UserServiceFactory;
import com.multysite.model.admin.ApplicationModel;
import com.multysite.util.Setting;

@SuppressWarnings("serial")
public class ListApplicationServlet extends HttpServlet {

	private static final Logger log = Logger
			.getLogger(ListApplicationServlet.class.getName());

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		try {
			if (NamespaceManager.get().equals(Setting.getGeneralNamespace())) {
				ApplicationModel model = new ApplicationModel();
				model.prepareApplicationByUser(UserServiceFactory
						.getUserService().getCurrentUser().getEmail());
				req.setAttribute("result", model.getListResult());
				req.getRequestDispatcher("/admin/pages/applications.jsp")
						.forward(req, resp);
			} else {
				int port = req.getServerPort();
				NamespaceManager.set(Setting.getGeneralNamespace());
				resp.sendRedirect("http://" + Setting.getDomain() + ":" + port
						+ "/applications");
			}
		} catch (Exception e) {
			e.printStackTrace();
			log.warning(e.toString());
			resp.sendError(4000,
					"Website is down for maintenance. We are sorry for the inconvenience !");
		}

	}
}
